<?php

define('CURSCRIPT', 'project');
require_once './core/init.inc.php';

if(!$_G['admin']->isLogged()){
	showmsg('您还未登陆！','index.php');
}

$navtitle = ' - 项目管理';

switch($_GET['action']){
case 'add':
	$id = max(0, intval($_GET['id']));
	if(!$id && !$_G['admin']->hasPermission('addproject')){
		showmsg('抱歉，您没有添加新项目的权限。', 'back');
	}elseif($id && !$_G['admin']->hasPermission('editproject')){
		showmsg('抱歉，您没有修改项目信息的权限。', 'back');
	}

	if($_POST){	
		$project = NULL;
	
		$data = array(
			'code' => $_POST['code'],
			'name' => $_POST['name'],
			'type' => intval($_POST['type']),
			'source' => $_POST['source'],
			'sourceaccount' => $_POST['sourceaccount'],
			'amount' =>intval($_POST['amount']),
			'creditedtime' => strtotime($_POST['creditedtime']),
			'specifieduses' => $_POST['specifieduses'],
			'responsor' => $_POST['responsor'],
			'responsormobile' => $_POST['responsormobile'],
			'deadline' => strtotime($_POST['deadline']),
			'restamount' => intval($_POST['amount']),
			'dateline' => $_G['timestamp'],
			'status' => 0,
		);
		
		if($id){
			$project = new Project($id);
			
			$data['restamount'] = $project->getProperty('restamount') + $data['amount'] - $project->getProperty('amount');
			if($data['restamount'] < 0){
				showmsg('余额不够！','back');
			}

			unset($data['dateline'], $data['status']);
		}
		if(Project::isRequiredItem('type') && empty($data['type'])){
			showmsg('请选择项目类型。','back');
		}
		if(Project::isRequiredItem('code') && empty($data['code'])){
			showmsg('项目代码不能为空。','back');
		}
		if(Project::isRequiredItem('name') && empty($data['name'])){
			showmsg('项目名称不能为空。','back');
		}
		if(Project::isRequiredItem('source') && empty($data['source'])){
			showmsg('捐赠单位不能为空。','back');
		}
		if(Project::isRequiredItem('amount') && (!$data['amount'] || $data['amount'] <= 0)){
			showmsg('捐赠数额不能为空或非正数。','back');
		}
		if(Project::isRequiredItem('creditedtime') && !$data['creditedtime']){
			showmsg('到账时间不能为空。','back');
		}
		if(Project::isRequiredItem('responsor') && empty($data['responsor'])){
			showmsg('项目负责人不能为空。','back');
		}
		if(Project::isRequiredItem('deadline') && empty($data['deadline'])){
			showmsg('项目截止时间不能为空。','back');
		}
		if(Project::isRequiredItem('responsormobile') && !intval($data['responsormobile'])){
			showmsg('项目负责人电话不能为空。','back');
		}
		
		if($id){//有id参数说明是编辑操作			
			$project->setProperty($data);

			$extmsg = '';
			if(!empty($_FILES['contrast']['name']) && !$project->uploadAttachment('contrast')){
				$extmsg.= '合同快照上传失败。';
			}
			if(!empty($_FILES['thankletter']['name']) && !$project->uploadAttachment('thankletter')){
				$extmsg.= '感谢信上传失败。';
			}
			
			showmsg('信息修改成功！'.$extmsg, 'project.php?action=more&id='.$id);
		
		}else{
			$id = Project::insertItem($data);
			
			if($id == -1){
				showmsg('项目代码重复。添加失败。', 'back');
			}elseif(!$id){
				showmsg('添加失败！', 'back');
			}else{
				$project = new Project($id);
				
				$extmsg = '';
				if(!empty($_FILES['contrast']['name']) && !$project->uploadAttachment('contrast')){
					$extmsg.= '合同快照上传失败。';
				}
				if(!empty($_FILES['thankletter']['name']) && !$project->uploadAttachment('thankletter')){
					$extmsg.= '感谢信上传失败。';
				}
				showmsg('添加成功！'.$extmsg, 'project.php?action=list');
			}
		}
	}else{
		$navtitle.= ' - 添加项目';
		include view('project_add');
	}
break;


case 'more':
	$id = intval($_GET['id']);
	$project = new Project($id);
	$process_type = Project::$process_type;
	$projectstatus = Project::$status;
	if($project->exists()){
		$project->getProcess();
	}else{
		showmsg('该项目不存在。', 'back');
	}

	include view('project_detail');
break;

case 'edit':
	if(!$_G['admin']->hasPermission('editproject')){
		showmsg('抱歉，您没有编辑项目信息的权限。', 'back');
	}
	$projectstatus = Project::$status;

	if($id = $_GET['id']){
		$detail=Project::GetItemById($id);
		include view('project_edit');
	
	}else{
		//统一到添加处理
		rheader('Location: project.php?action=add');
	}
	
break;
case 'delete':
	if(!$_GET['confirm']){
		showmsg("您确定真的要删除项目\"$_GET[name]\"吗？", 'confirm');
	}elseif($id = intval($_GET['id'])){
		Project::deleteItem($id);
		showmsg('项目已被成功删除！', 'project.php?action=list');
	}
break;

case 'search':
	if($_POST){
		$conditions = array();
		
		foreach(array('name','responsor','source','sourceaccount','specifieduses') as $item){
			if($_POST['check_'.$item] && !empty($_POST['project_'.$item])){
				$conditions[] = 'CONVERT(`'.$item.'` USING utf8) LIKE \'%'.$_POST['project_'.$item].'%\'';
			}
		}
		foreach(array('code','type','status','responsormobile') as $item){
			$value = intval($_POST['project_'.$item]);
			if($_POST['check_'.$item] && $value){
				$conditions[] = '`'.$item.'`='.$value;
			}
		}
		foreach(array('dateline','deadline','creditedtime') as $item){
			if($_POST['check_'.$item] && (!empty($_POST['project_'.$item.'_from']) || !empty($_POST['project_'.$item.'_to']))){
				$from = strtotime($_POST['project_'.$item.'_from']);
				$to = strtotime($_POST['project_'.$item.'_to']);
				$conditions[] = '`'.$item.'`>='.$from;
				$conditions[] = '`'.$item.'`<='.$to;
			}
		}
		foreach(array('amount','restamount') as $item){
			if($_POST['check_'.$item] && !empty($_POST['project_'.$item])){
				$from = floatval($_POST['project_'.$item.'_from']);
				$to = floatval($_POST['project_'.$item.'_to']);
				$conditions[] = '`'.$item.'`>='.$from;
				$conditions[] = '`'.$item.'`<='.$to;
			}
		}
		
		if(!$conditions){
			showmsg('抱歉，您未输入任何搜索条件。', 'project.php?action=list');
		}
		
		$projectlist = Project::getItemByMultiConditions(array('id','code','type','name','dateline','responsor','amount','restamount'), $conditions);
		
		$pageNum = count($projectlist);
		writecache('search_'.$_G['admin']->v['uid'], $projectlist);
		$_GET['action'] = 'searchresult';
	}else{
		include view('project_search');
		exit();
	}

case 'searchresult':
	if(!$_POST){
		$projectlist = readcache('search_'.$_G['admin']->v['uid']);
	}
	
	$limit = 20;
	$offset = ($page - 1) * $limit;
	$pageNum = count($projectlist);

	$projectlist = array_slice($projectlist, $offset, $limit);

	include view('project_list');

break;

case 'quicksearch':
	if($_POST['searchmode'])$conditions='`name`=\''.$_POST['quicksearch'].'\'';
	else $conditions='`code`='.intval($_POST['quicksearch']);
	$projectlist = Project::getItemByMultiConditions(array('id','code','type','name','responsor','amount','restamount'), $conditions);
	writecache('search_'.$_G['admin']->v['uid'], $projectlist);
	$pageNum = count($projectlist);
	
	$_GET['action'] = 'searchresult';
	$limit = 20;
	$offset = ($page - 1) * $limit;
	$projectlist = array_slice($projectlist, $offset, $limit);
	
	include view('project_list');
break;

case 'addprocess':
	if(!$_G['admin']->hasPermission('addprocess')){
		showmsg('抱歉，您没有添加项目进程的权限。', 'back');
	}
	if($_POST)
	{
		$data = array(
			'pid' => intval($_GET['id']),
			'dateline' => strtotime($_POST['process_dateline']),
			'amount' => intval($_POST['process_amount']),
			'type' => intval($_POST['process_type']),
			'reason' => $_POST['process_reason'],
			'consumer' => $_POST['process_consumer'],
			'responsor' => $_POST['process_responsor']
		);
		if(!$data['pid']){
			showmsg('param error!', 'back');
		}
		else if(!$data['dateline']){
			showmsg('请输入交易时间', 'back');
		}
		elseif(!$data['amount']){
			showmsg('请输入出入金额', 'back');
		}
		else if(!$data['type']){
			showmsg('请输入出入类型', 'back');
		}
		else if(!$data['reason']){
			showmsg('请输入缘由', 'back');
		}
		else if(!$data['consumer']){
			showmsg('请输入用款单位', 'back');
		}
		else if(!$data['responsor']){
			showmsg('请输入经办人', 'back');
		}
		$project = new Project();
		$project->fetchById($data['pid']);
		$process_type_operator = Project::$process_type_operator;
		if($project->exists()){
			if($process_type_operator[$data['type']]=='-'&&$project->getProperty('restamount') < $data['amount']){
				showmsg('项目余额不足，仅余'.$project->getProperty('restamount').'，添加失败。', 'back');
			}
			if(!$_GET['confirm']){
				showmsg('您确认添加此项目进程到该项目下吗？<br />项目代码：'.$project->getProperty('code').'<br />项目名称：'.$project->getProperty('name').'<br />项目余额：'.$project->getProperty('restamount'), 'confirm');
			}else{
				$project->addProcess($data);
				showmsg('成功添加项目进程！', 'project.php?action=more&id='.$data['pid']);
			}
		
		}else{
			showmsg('该项目不存在，请确认并重新输入项目代码。', 'back');
		}
	}
	include view('project_addprocess');
		
break;

case 'deleteprocess':
	if(!$_G['admin']->hasPermission('deleteprocess')){
		showmsg('抱歉，您没有删除项目进程的权限。', 'back');
	}

	if(!$id = intval($_GET['id'])){
		showmsg('非法操作，ID不合法。', 'back');
	}
	
	if(!$_GET['confirm']){
		showmsg('您确定要删除该条进程吗？', 'confirm');
	}else{
		$project = new Project();
		
		if(!$project->fetchByProcessId($id)){
			showmsg('没有找到对应的项目，可能被其他管理员删除。', 'back');
		}
		
		if($project->deleteProcess($id) > 0){
			showmsg('成功删除该进程。', 'refresh');
		}else{
			showmsg('删除失败，该进程已不存在。', 'refresh');
		}
	}
break;

case 'list':default:
	$pageNum = Project::getMaxNum('1');
	$page = max(1, min($page, $pageNum));
	$limit = 10;
	$offset = ($page - 1) * $limit;
	
	$projectlist = Project::getItemOrderByTime(array('id','name','creditedtime','code','responsormobile','type','responsor','amount','restamount','source'), $offset, $limit);
	$projectstatus = Project::$status;
	include view('project_list');
break;
case 'editprocess':
	if(isset($_GET['id']))$project_id = $_GET['id'];
	else showmsg('param error!','back');//异常
	$process_type = Project::$process_type;
	
	include view('project_editprocess');
break;
}

?>